package com.gxy.learn.securityauth.auth;

import com.alibaba.fastjson.JSONObject;
import com.gxy.learn.securityauth.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description(无权访问处理类)
 * @author: Gao xueyong
 * Create at: 2021/12/1 下午9:22
 */
@Slf4j
@Component
public class SecurityAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        log.error("无权限访问");
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSONObject.toJSONString(Result.error("无权限访问")));
    }
}
